<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
      >
      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>
    <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
      <Page
        style="margin-right: 0px"
        :current="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :total="total"
        @on-change="pageChange"
        simple
      ></Page>
    </el-col>

    <el-table v-loading="loading" :data="StorageList">
      <el-table-column label="大分类名称" align="center" prop="lbmLbmc" />
      <el-table-column label="大分类编码" align="center" prop="lbmLbdm" />
      <el-table-column label="客户名称" width="230">
        <template #default="scope">
          <el-select
            class="inputName"
            v-model="scope.row.cusCode"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="dict in cus"
              :key="dict.cusId"
              :label="dict.cusName"
              :value="dict.cusCode"
            >
              <span style="float: left">{{ dict.cusName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                dict.cusCode
              }}</span>
            </el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-hasPermi="['repertory:StorageManage:assign']"
          >
            分配仓位
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分配仓位弹窗-->
    <el-dialog
      :title="title"
      v-model="openSt"
      :close-on-click-modal="false"
      draggable
      width="800px"
      append-to-body
    >
      <el-form
        :model="queryStore"
        ref="queryFormstr"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="仓库代码" prop="storageCode">
          <el-input
            v-model="queryStore.storageCode"
            placeholder="请输入仓库代码"
            clearable
            size="small"
            @keyup.enter.native="handleQueryStr"
          />
        </el-form-item>
        <el-form-item label="仓位名称" prop="storageName">
          <el-input
            v-model="queryStore.storageName"
            placeholder="请输入仓位名称"
            clearable
            size="small"
            @keyup.enter.native="handleQueryStr"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQueryStr"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryStr"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table :data="StorageList2" @current-change="clickChange" ref="Table">
        <el-table-column label="选择" width="55">
          <template #default="scope">
            <el-radio v-model="tableRadio" :label="scope.row"><i></i></el-radio>
          </template>
        </el-table-column>
        <el-table-column label="仓库代码" align="center" prop="storageCode" />
        <el-table-column label="仓位名称" align="center" prop="storageName" />
        <el-table-column
          label="默认储位代码"
          align="center"
          prop="storageDefaultstorage"
        />
      </el-table>
      <pagination
        v-if="totalSearch > 0"
        :total="totalSearch"
        :page.sync="queryStore.pageNum"
        :limit.sync="queryStore.pageSize"
        @pagination="getListStr"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStorageForm">确 定</el-button>
        <el-button @click="openSt = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStorage } from "@/api/repertory/Storage";
import { getAllCustomer, getAllCustomerSY } from "@/api/project/customer";
import {
  changeWarehouse,
  listStorageManage,
} from "@/api/repertory/StorageManage";

export default {
  name: "StorageManage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,

      //当前数据
      nowData: null,
      // 选中数组
      ids: [],
      storageName: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 储位资料表格数据
      StorageList: [],
      StorageList2: [],
      //储位页数
      totalSearch: 0,
      //分配仓位窗口
      openSt: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      cusCode: null,
      //有效客户
      cus: [],
      //单选
      tableRadio: false,
      storageId: null,
      queryStore: {
        pageNum: 1,
        pageSize: 10,
        storageCode: null,
        storageName: null,
        storageFlag: "0",
      },
      // 有效标识字典
      storageFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storageCode: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
        storageName: [
          { required: true, message: "仓位名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    getAllCustomerSY().then((response) => {
      this.cus = response.data;
    });
  },
  methods: {
    /** 查询储位资料列表 */
    getList() {
      this.loading = true;
      listStorageManage(this.queryParams).then((response) => {
        this.StorageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListStr() {
      listStorage(this.queryStore).then((response) => {
        this.StorageList = response.rows;
        this.totalSearch = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQueryStr() {
      this.getListStr();
    },
    /** 重置按钮操作 */
    resetQueryStr() {
      this.resetForm("queryFormstr");
      this.handleQueryStr();
    },

    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleAssign(row) {
      if (row.cusCode) {
        this.nowData = row;
        listStorage({ storageFlag: "0" }).then((response) => {
          this.StorageList2 = response.rows;
          this.totalSearch = response.total;
          //获取数据
          this.openSt = true;
          this.title = "分配仓位";
        });
      } else {
        this.msgError("请选择客户");
      }
    },

    clickChange(item) {
      if (item == null) {
        return;
      }
      this.tableRadio = item;
      this.storageId = item.storageId;
    },

    submitStorageForm() {
      const data = {
        lbmCode: this.nowData.lbmLbdm,
        cusCode: this.nowData.cusCode,
        storageId: this.storageId,
      };

      changeWarehouse(data).then((res) => {
        if (res) {
          this.msgSuccess(
            "分配成功,详细记录在[工程管理-工程报表管理-编码规则匹配规则当中]"
          );
          this.openSt = false;
          this.getList();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .inputName {
  > .el-input__inner {
    height: 20px;
  }
  > .el-input > .el-input__inner {
    height: 20px;
  }
  > .el-input
    > .el-input__suffix
    > .el-input__suffix-inner
    > .el-select__caret {
    line-height: 24px;
  }
}
</style>
